using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;
namespace Microsoft.Phone.Controls.Maps
{
	public class TileSource : INotifyPropertyChanged
	{
		public const string UriSchemeUriFragment = "{UriScheme}";
		public const string QuadKeyUriFragment = "{quadkey}";
		public const string SubdomainUriFragment = "{subdomain}";
		private const string InternalQuadKeyUriFragment = "{QUADKEY}";
		private const string InternalSubdomainUriFragment = "{SUBDOMAIN}";
		private string convertedUriFormat;
		private int maxX;
		private int maxY;
		private string[][] subdomainsList;
		private string uriFormat;
		public event PropertyChangedEventHandler PropertyChanged
		{
			[MethodImpl(32)]
			add
			{
				this.PropertyChanged = (PropertyChangedEventHandler)Delegate.Combine(this.PropertyChanged, value);
			}
			[MethodImpl(32)]
			remove
			{
				this.PropertyChanged = (PropertyChangedEventHandler)Delegate.Remove(this.PropertyChanged, value);
			}
		}
		public string UriFormat
		{
			get
			{
				return this.uriFormat;
			}
			set
			{
				if (this.uriFormat != value)
				{
					this.uriFormat = value;
					this.convertedUriFormat = TileSource.ReplaceString(this.uriFormat, "{UriScheme}", "HTTP");
					this.convertedUriFormat = TileSource.ReplaceString(this.convertedUriFormat, "{quadkey}", "{QUADKEY}");
					this.convertedUriFormat = TileSource.ReplaceString(this.convertedUriFormat, "{subdomain}", "{SUBDOMAIN}");
					this.OnPropertyChanged("UriFormat");
				}
			}
		}
		public TileSource()
		{
			this.subdomainsList = new string[][]
			{
				new string[]
				{
					"0",
					"2",
					"4",
					"6"
				},
				new string[]
				{
					"1",
					"3",
					"5",
					"7"
				}
			};
			this.maxX = 2;
			this.maxY = 4;
		}
		public TileSource(string uriFormat) : this()
		{
			this.UriFormat = uriFormat;
		}
		public virtual Uri GetUri(int x, int y, int zoomLevel)
		{
			Uri result = null;
			QuadKey quadKey = new QuadKey(x, y, zoomLevel);
			if (!string.IsNullOrEmpty(this.convertedUriFormat) && !string.IsNullOrEmpty(quadKey.Key))
			{
				result = new Uri(this.convertedUriFormat.Replace("{QUADKEY}", quadKey.Key).Replace("{SUBDOMAIN}", this.GetSubdomain(quadKey)));
			}
			return result;
		}
		public virtual string GetSubdomain(QuadKey quadKey)
		{
			if (this.subdomainsList == null)
			{
				return string.Empty;
			}
			return this.subdomainsList[quadKey.X % this.maxX][quadKey.Y % this.maxY];
		}
		public void SetSubdomains(string[][] subdomains)
		{
			if (subdomains == null)
			{
				this.subdomainsList = null;
				return;
			}
			if (subdomains.Length != 0 && subdomains[0].Length != 0)
			{
				int num = subdomains[0].Length;
				for (int i = 0; i < subdomains.Length; i++)
				{
					string[] array = subdomains[i];
					if (array.Length != num)
					{
						throw new ArgumentException(ExceptionStrings.TileSource_InvalidSubdomains_DifferentLength);
					}
					string[] array2 = array;
					for (int j = 0; j < array2.Length; j++)
					{
						string text = array2[j];
						if (text == null)
						{
							throw new ArgumentException(ExceptionStrings.TileSource_InvalidSubdomain_stringNull);
						}
					}
				}
				this.subdomainsList = subdomains;
				this.maxX = subdomains.Length;
				this.maxY = num;
				return;
			}
			throw new ArgumentException(ExceptionStrings.TileSource_InvalidSubdomains_LengthMoreThan0);
		}
		private static string ReplaceString(string input, string pattern, string replacement)
		{
			return Regex.Replace(input, pattern, replacement, 1);
		}
		protected void OnPropertyChanged(string propertyName)
		{
			PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
			if (propertyChanged != null)
			{
				PropertyChangedEventArgs propertyChangedEventArgs = new PropertyChangedEventArgs(propertyName);
				propertyChanged.Invoke(this, propertyChangedEventArgs);
			}
		}
	}
}
